package com.lfg.leetcode.leetcode100;

import com.lfg.leetcode.leetcode0.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class No113pathSum {

    public static void main(String[] args) {
        List<List<Integer>> ret = pathSum(TreeNode.stringToTreeNode("[1,0,1,1,2,0,-1,0,1,-1,0,-1,0,1,0]"), 2);
        System.out.println("[" + ret + "]");
    }

    public static List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> ret = new ArrayList<>();
        pathSum(root, 0, sum, ret, new ArrayList<>());
        return ret;
    }

    public static void pathSum(TreeNode root, int cnt, int sum, List<List<Integer>> ret, List<Integer> data) {
        if (root == null) {
            return;
        }

        int total = root.val + cnt;
        if (root.right == null && root.left == null) {
            if (total == sum) {
                data.add(root.val);
                ret.add(new ArrayList<>(data));
                data.remove(data.size() - 1);
                return;
            }
            return;
        }
        data.add(root.val);
        pathSum(root.left, total, sum, ret, data);
        pathSum(root.right, total, sum, ret, data);
        data.remove(data.size() - 1);
    }

}
